package concurrent;

/**
 * 测试java Volatile 关键字的作用
 * @author fengpeng
 * @date 2019-06-24  23:06
 */
public class TestVolatile  extends Thread{

    private  boolean flag = true;

    public void setFlag(boolean flag){
        this.flag = false;
    }

    @Override
    public void run(){
        System.out.println("开始执行run方法！");

        while (flag){
            /*空转不执行*/
        }
        System.out.println("flag 置为 false 跳出死循环！");
    }

    public static void main(String[] args) throws InterruptedException {
        TestVolatile tv = new TestVolatile();
        tv.start();
        tv.sleep(3000L);
        tv.setFlag(false);
        tv.sleep(1000L);
        System.out.println("此时flag值为： "+tv.flag);
    }
}
